package com.mifengkong.kotlincommon.recyclerview.wrap

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.View

/**
 * Created by jack on 2018/6/20
 */
open class WrapRecyclerView : RecyclerView {

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle)

    //包裹了一层头部和底部的Adapter
    private lateinit var mWrapRecyclerAdapter: WrapRecyclerAdapter

    //列表数据的Adapter
    private var mAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = null

    override fun setAdapter(adapter: Adapter<RecyclerView.ViewHolder>) {
        if (mAdapter != null) {
            mAdapter?.unregisterAdapterDataObserver(mDataObserver)
            mAdapter = null
        }

        this.mAdapter = adapter

        mWrapRecyclerAdapter = adapter as? WrapRecyclerAdapter ?: WrapRecyclerAdapter(adapter)

        super.setAdapter(mWrapRecyclerAdapter)

        // 注册一个观察者 不然更新没有效果
        mAdapter?.registerAdapterDataObserver(mDataObserver)

        // 解决GridLayout添加头部和底部也要占据一行
        mWrapRecyclerAdapter.adjustSpanSize(this)

    }

    // 添加头部
    fun addHeaderView(view: View) {
        // 如果没有Adapter那么就不添加，也可以选择抛异常提示
        // 让他必须先设置Adapter然后才能添加，这里是仿照ListView的处理方式
        mWrapRecyclerAdapter.addHeaderView(view)
    }

    // 添加底部
    fun addFooterView(view: View) {
        mWrapRecyclerAdapter.addFooterView(view)
    }

    // 移除头部
    fun removeHeaderView(view: View) {
        mWrapRecyclerAdapter.removeHeaderView(view)
    }

    // 移除底部
    fun removeFooterView(view: View) {
        mWrapRecyclerAdapter.removeFooterView(view)
    }

    private val mDataObserver = object : RecyclerView.AdapterDataObserver() {
        override fun onChanged() {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyDataSetChanged()
            }
        }

        override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyItemRemoved(positionStart)
            }
        }

        override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyItemMoved(fromPosition, toPosition)
            }
        }

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyItemChanged(positionStart)
            }
        }

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyItemChanged(positionStart, payload)
            }
        }

        override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
            if (null == mAdapter) {
                return
            }
            if (mWrapRecyclerAdapter !== mAdapter) {
                mWrapRecyclerAdapter.notifyItemInserted(positionStart)
            }
        }
    }
}